package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.CheckBox;
import com.cy.pj.sys.dao.SysRoleDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.SysRole;
import com.cy.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(rollbackFor = Throwable.class,
        isolation = Isolation.READ_COMMITTED,
        timeout = 60
)
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleDao sysRoleDao;
    private SysRoleMenuDao sysRoleMenuDao;

    @Transactional(readOnly = true)
    @Override
    public List<SysRole> findRoles(SysRole entity) {
        return sysRoleDao.selectRoles(entity);
    }

    @Transactional
    @Override
    public int saveRole(SysRole entity) {
        //保存角色自身信息
         int rows = sysRoleDao.insertRole(entity);
         //新增角色菜单关系数据库
         sysRoleMenuDao.insertRoleMenus(entity.getId(), entity.getMenuIds());
         return rows;
    }

    @Override
    public SysRole findById(Integer id) {
        return sysRoleDao.selectById(id);
    }
    @Transactional(readOnly = true)
    @Override
    public int updateRole(SysRole entity) {
        //更新角色自身信息
        int rows = sysRoleDao.updateRole(entity);
        if (rows ==0)throw new ServiceException("记录可能已经不存在了");
        //更新角色菜单关系数据,
        // 1先删除原有关系,
        sysRoleMenuDao.deleteByRoleId(entity.getId());
        // 2再添加新的关系
        sysRoleMenuDao.insertRoleMenus(entity.getId(),entity.getMenuIds());
        return  rows;
    }

    @Transactional(readOnly = true)
    @Override
    public List<CheckBox> findCheckRoles() {
        return sysRoleDao.selectCheckRoles();
    }
}
